package com.sllp.C71S3TsllpShop.web.back;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.BusinessBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.FavoriteBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.NoticeBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersItemBizImpl;

@Controller
@SessionAttributes({"loginPeople"})
public class BackIndexAction {
	
	@Resource
	private FavoriteBizImpl fbi;
	@Resource
	private NoticeBizImpl nbi;
	@Resource
	private OrdersItemBizImpl oibi;
	@Resource
	private BusinessBizImpl bbi;

	@GetMapping({"/back","/back/index","/back/index.*"})
	public String toIndex(@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,
			Model m) {
		if(user != null && business != null) {
			// 商家登录
			m.addAttribute("loginPeople", 1);
			m.addAttribute("loginUser", user);
			m.addAttribute("loginBusiness", business);
			
			List<Favorite> favorites = fbi.findFavoriteWithNull();
			int sellCount = 0; // 总销量
			int todySellCount = 0; // 今日销量
			int favoriteCount = 0; // 总收藏数
			List<OrdersItem> ordersItems = new ArrayList<OrdersItem>();
			if(business.getProducts().size()>0) {
				// 销量计算
				for(Product product : business.getProducts()) {
					// 总销量
					if(product.getBuyNum()!=null) {
						sellCount += product.getBuyNum();
					}
					// 收藏数
					if(favorites.size()>0) {
						for(Favorite favorite : favorites) {
							if(favorite.getProduct().equals(product.getId())) {
								favoriteCount ++;
							}
						}
					}
					// 今日销量
					// 先获取今天0点的时间的long值
					long now = System.currentTimeMillis() / 1000l; 
					long daySecond = 60 * 60 * 24; 
					long dayTime = now - (now + 8 * 3600) % daySecond;
					for(Specs specs : product.getSpecs()) {
						for(OrdersItem ordersItem : specs.getOrdersItems()) {
							if(ordersItem.getState()==2 || ordersItem.getState()==5) {
								ordersItems.add(ordersItem);
							}
							// 比较订单的时间和今天0点的时间
							if(dayTime < (ordersItem.getOrders2().getOrderTime().getTime()/1000l)) {
								todySellCount++;
							}
						}
					}
				}
			}
			m.addAttribute("sellCount", sellCount);
			m.addAttribute("todySellCount", todySellCount);
			m.addAttribute("favoriteCount", favoriteCount);
			
			m.addAttribute("noticeList", nbi.findNotice(2));
			m.addAttribute("ordersItemList", ordersItems);
			
			m.addAttribute("indexChartData", bbi.generateIndexChartData(business));
			
		}else if(admin != null){
			// 管理员登录
			m.addAttribute("loginPeople", 2);
			m.addAttribute("loginAdmin", admin);
			
			// 整个网站的销量 收藏
			int sellCount = 0; // 总销量
			int todySellCount = 0; // 今日销量
			int favoriteCount = 0; // 总收藏数
			List<OrdersItem> ordersItems = oibi.selectAll();
			sellCount = ordersItems.size();
			
			// 今日销量
			// 先获取今天0点的时间的long值
			long now = System.currentTimeMillis() / 1000l; 
			long daySecond = 60 * 60 * 24; 
			long dayTime = now - (now + 8 * 3600) % daySecond;
			long nextDayTime = dayTime+(60l*60l*24l);
			for(OrdersItem ordersItem : ordersItems) {
				// 比较订单的时间和今天0点的时间
				long orderTime = ordersItem.getOrders2().getOrderTime().getTime()/1000l;
				if(dayTime < orderTime && orderTime < nextDayTime) {
					todySellCount++;
				}
			}
			favoriteCount=fbi.findFavoriteWithNull().size();
			
			m.addAttribute("sellCount", sellCount);
			m.addAttribute("todySellCount", todySellCount);
			m.addAttribute("favoriteCount", favoriteCount);
			
			m.addAttribute("noticeList", nbi.findNotice(1));
			
			m.addAttribute("indexChartData", oibi.generateIndexChartData());
			
		}else {
			
			return "redirect:toLogin.do";
		}
		return "back/index";
	}
	
	@GetMapping("back/updateOrdersItemState.do")
	public String updateOrdersItemState(int state, String id, Model m,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business) {
		OrdersItem ordersItem = new OrdersItem();
		ordersItem.setId(id);
		if(state == 5) {
			state = 6;
		}else if(state == 2) {
			state = 3;
		}
		ordersItem.setState(state);
		oibi.updateOrdersItemState(ordersItem);
		Business loginBusiness = bbi.selectByPrimaryKey(business.getId()).get(0);
		m.addAttribute("loginBusiness", loginBusiness);
		
		return "back/index::ordersItemTable";
	}
	
	@GetMapping("back/loginOut.do")
	public String loginOut(@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,HttpSession session,
			Model m) {
		if(user != null && business != null) {
			session.removeAttribute("backLoginBusiness");
			session.removeAttribute("loginUser");
		}else if(admin != null){
			session.removeAttribute("backLoginAdmin");
		}
		
		return "redirect:/back/toLogin.do";
	}
}
